package com.youyi.yychosesdk.models.sticker.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.lkx.liandianqinew.R;
import com.youyi.yychosesdk.models.sticker.cache.StickerCache;
import com.youyi.yychosesdk.models.sticker.listener.OnStickerClickListener;
import java.util.Objects;

public class BitmapSticker extends View {
    private Bitmap btDelete;
    private Bitmap btMirror;
    private Bitmap btScale;
    private int btSize;
    private String cacheKey;
    private ClickType clickType;
    private float downX1;
    private float downX2;
    private float downY1;
    private float downY2;
    private float[] dstPs;
    private Paint framePaint;
    private GestureDetector gestureDetector;
    private Bitmap image;
    private int imageHeight;
    private int imageWidth;
    public boolean isChecked = false;
    private boolean isOut = false;
    private boolean isUsing = true;
    private float lastDegree;
    private float lastDoubleDegress;
    private OnStickerClickListener listener;
    private Matrix mMatrix;
    private Paint mPaint;
    private boolean mirror = true;
    private Bitmap mirrorImage;
    private Path path;
    private Bitmap srcImage;
    private float[] srcPs;
    private int startX;
    private int startY;

    /* access modifiers changed from: private */
    public enum ClickType {
        DELETE,
        MIRROR,
        SCALE,
        ROTATE,
        IMAGE,
        OUT
    }

    public BitmapSticker(Context context, String str, int i, int i2, int i3) {
        super(context);
        if (TextUtils.isEmpty(str)) {
            this.image = StickerCache.get().getSrcBitmap(getResources(), i);
            this.cacheKey = String.valueOf(i);
        } else {
            this.image = StickerCache.get().getSrcBitmap(str);
            this.cacheKey = str;
        }
        this.path = new Path();
        this.srcImage = this.image;
        this.mirrorImage = StickerCache.get().getMirrorBitmap(this.cacheKey);
        this.imageWidth = this.image.getWidth();
        int height = this.image.getHeight();
        this.imageHeight = height;
        int i4 = i2 - (this.imageWidth / 2);
        this.startX = i4;
        if (i4 < 100) {
            this.startX = i2 / 2;
        }
        int i5 = i3 - (height / 2);
        this.startY = i5;
        if (i5 < 100) {
            this.startY = i3 / 2;
        }
        initButtons();
        initPs();
        initPaints();
        initMatrix();
        initCanvasPosition();
        this.lastDegree = computeDegree(new Point(this.imageWidth, this.imageHeight), new Point(this.imageWidth / 2, this.imageHeight / 2));
        this.lastDoubleDegress = 1000.0f;
        this.gestureDetector = new GestureDetector(context, new StickerGestureListener());
    }

    public void setOnStickerClickListener(OnStickerClickListener onStickerClickListener) {
        this.listener = onStickerClickListener;
    }

    private void initCanvasPosition() {
        this.mMatrix.postTranslate((float) this.startX, (float) this.startY);
        this.mMatrix.mapPoints(this.dstPs, this.srcPs);
    }

    private void initMatrix() {
        this.mMatrix = new Matrix();
    }

    private void initPaints() {
        Paint paint = new Paint();
        this.mPaint = paint;
        paint.setAntiAlias(true);
        this.mPaint.setDither(true);
        this.mPaint.setFilterBitmap(true);
        Paint paint2 = new Paint();
        this.framePaint = paint2;
        paint2.setAntiAlias(true);
        this.framePaint.setStrokeWidth(1.0f);
        this.framePaint.setColor(ContextCompat.getColor(getContext(), 17170443));
    }

    private void initPs() {
        int i = this.imageWidth;
        int i2 = this.imageHeight;
        float[] fArr = {0.0f, 0.0f, (float) i, 0.0f, (float) i, (float) i2, 0.0f, (float) i2, (float) (i / 2), (float) (i2 / 2)};
        this.srcPs = fArr;
        this.dstPs = (float[]) fArr.clone();
    }

    private void initButtons() {
        this.btDelete = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_delete_easy_photos);
        this.btMirror = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_mirror_easy_photos);
        this.btScale = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_controller_easy_photos);
        this.btSize = this.btDelete.getWidth();
    }

    /* access modifiers changed from: protected */
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(this.image, this.mMatrix, this.mPaint);
        if (this.isUsing) {
            drawOthers(canvas);
        }
    }

    private void drawOthers(Canvas canvas) {
        this.path.reset();
        Path path2 = this.path;
        float[] fArr = this.dstPs;
        path2.moveTo(fArr[0], fArr[1]);
        Path path3 = this.path;
        float[] fArr2 = this.dstPs;
        path3.lineTo(fArr2[2], fArr2[3]);
        Path path4 = this.path;
        float[] fArr3 = this.dstPs;
        path4.lineTo(fArr3[4], fArr3[5]);
        Path path5 = this.path;
        float[] fArr4 = this.dstPs;
        path5.lineTo(fArr4[6], fArr4[7]);
        Path path6 = this.path;
        float[] fArr5 = this.dstPs;
        path6.lineTo(fArr5[0], fArr5[1]);
        int i = 0;
        while (true) {
            if (i >= 7) {
                break;
            } else if (i == 6) {
                float[] fArr6 = this.dstPs;
                canvas.drawLine(fArr6[i], fArr6[i + 1], fArr6[0], fArr6[1], this.framePaint);
                break;
            } else {
                float[] fArr7 = this.dstPs;
                int i2 = i + 2;
                canvas.drawLine(fArr7[i], fArr7[i + 1], fArr7[i2], fArr7[i + 3], this.framePaint);
                i = i2;
            }
        }
        Bitmap bitmap = this.btDelete;
        float[] fArr8 = this.dstPs;
        float f = fArr8[2];
        int i3 = this.btSize;
        canvas.drawBitmap(bitmap, f - ((float) (i3 / 2)), fArr8[3] - ((float) (i3 / 2)), this.mPaint);
        Bitmap bitmap2 = this.btMirror;
        float[] fArr9 = this.dstPs;
        float f2 = fArr9[0];
        int i4 = this.btSize;
        canvas.drawBitmap(bitmap2, f2 - ((float) (i4 / 2)), fArr9[1] - ((float) (i4 / 2)), this.mPaint);
        Bitmap bitmap3 = this.btScale;
        float[] fArr10 = this.dstPs;
        float f3 = fArr10[4];
        int i5 = this.btSize;
        canvas.drawBitmap(bitmap3, f3 - ((float) (i5 / 2)), fArr10[5] - ((float) (i5 / 2)), this.mPaint);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        this.gestureDetector.onTouchEvent(motionEvent);
        if (1 == motionEvent.getAction() || 6 == motionEvent.getAction() || 6 == motionEvent.getAction() || 262 == motionEvent.getAction()) {
            setDoubleDownPoints(0.0f, 0.0f, 0.0f, 0.0f);
            this.lastDoubleDegress = 1000.0f;
            float[] fArr = this.dstPs;
            Point point = new Point((int) fArr[4], (int) fArr[5]);
            float[] fArr2 = this.dstPs;
            this.lastDegree = computeDegree(point, new Point((int) fArr2[8], (int) fArr2[9]));
        }
        return !this.isOut;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setDoubleDownPoints(float f, float f2, float f3, float f4) {
        this.downX1 = f;
        this.downY1 = f2;
        this.downX2 = f3;
        this.downY2 = f4;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void calculateClickType(int i, int i2) {
        int i3 = this.btSize;
        RectF rectF = new RectF((float) ((i - (i3 / 2)) - 40), (float) ((i2 - (i3 / 2)) - 40), (float) ((i3 / 2) + i + 40), (float) ((i3 / 2) + i2 + 40));
        float[] fArr = this.dstPs;
        if (rectF.contains(fArr[2] - 20.0f, fArr[3])) {
            this.clickType = ClickType.DELETE;
            return;
        }
        float[] fArr2 = this.dstPs;
        if (rectF.contains(fArr2[0], fArr2[1])) {
            this.clickType = ClickType.MIRROR;
            return;
        }
        float[] fArr3 = this.dstPs;
        if (rectF.contains(fArr3[4] + 20.0f, fArr3[5])) {
            this.clickType = ClickType.SCALE;
            return;
        }
        float[] fArr4 = this.dstPs;
        if (rectF.contains(fArr4[6] - 20.0f, fArr4[7])) {
            this.clickType = ClickType.IMAGE;
            return;
        }
        RectF rectF2 = new RectF();
        this.path.computeBounds(rectF2, true);
        Region region = new Region();
        region.setPath(this.path, new Region((int) rectF2.left, (int) rectF2.top, (int) rectF2.right, (int) rectF2.bottom));
        if (region.contains(i, i2)) {
            if (this.isOut) {
                this.isOut = false;
            }
            if (!this.isUsing) {
                this.isUsing = true;
                this.listener.onUsing();
                postInvalidate();
            }
            this.clickType = ClickType.IMAGE;
            return;
        }
        if (this.isUsing) {
            this.isUsing = false;
            postInvalidate();
        }
        if (!this.isOut) {
            this.isOut = true;
        }
        this.clickType = ClickType.OUT;
    }

    public void delete() {
        Objects.requireNonNull(this.listener, "OnStickerClickListener listener is null");
        setVisibility(View.GONE);
        StickerCache.get().recycle(this.cacheKey);
        this.listener.onDelete();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void mirror() {
        boolean z = this.mirror;
        if (z) {
            this.image = this.mirrorImage;
        } else {
            this.image = this.srcImage;
        }
        this.mirror = !z;
        matrixMap();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void top() {
        bringToFront();
        invalidate();
        this.listener.onTop();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void move(float f, float f2) {
        this.mMatrix.postTranslate(f, f2);
        matrixMap();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void controller(MotionEvent motionEvent) {
        scale(motionEvent);
        rotate(motionEvent);
    }

    private void scale(MotionEvent motionEvent) {
        float f;
        float f2;
        float f3;
        float f4;
        float f5;
        float f6;
        float f7;
        float f8;
        if (motionEvent.getPointerCount() == 2) {
            f8 = this.downX2;
            f5 = this.downY2;
            f4 = this.downX1;
            f3 = this.downY1;
            f2 = motionEvent.getX(1);
            f7 = motionEvent.getY(1);
            f = motionEvent.getX(0);
            f6 = motionEvent.getY(0);
        } else {
            float[] fArr = this.dstPs;
            float f9 = fArr[4];
            float f10 = fArr[5];
            float f11 = fArr[0];
            f3 = fArr[1];
            f2 = motionEvent.getX();
            f7 = motionEvent.getY();
            f = f11;
            f8 = f9;
            f5 = f10;
            f4 = f;
            f6 = f3;
        }
        float distanceOfTwoPoints = getDistanceOfTwoPoints(f2, f7, f, f6) / getDistanceOfTwoPoints(f8, f5, f4, f3);
        if (getScaleValue() >= 0.3f || distanceOfTwoPoints >= 1.0f) {
            Matrix matrix = this.mMatrix;
            float[] fArr2 = this.dstPs;
            matrix.postScale(distanceOfTwoPoints, distanceOfTwoPoints, fArr2[8], fArr2[9]);
            matrixMap();
            if (motionEvent.getPointerCount() == 2) {
                setDoubleDownPoints(f, f6, f2, f7);
            }
        }
    }

    private void rotate(MotionEvent motionEvent) {
        if (motionEvent.getPointerCount() == 2) {
            float computeDegree = computeDegree(new Point((int) motionEvent.getX(0), (int) motionEvent.getY(0)), new Point((int) motionEvent.getX(1), (int) motionEvent.getY(1)));
            if (this.lastDoubleDegress == 1000.0f) {
                this.lastDoubleDegress = computeDegree;
            }
            float[] fArr = this.dstPs;
            this.mMatrix.postRotate(computeDegree - this.lastDoubleDegress, fArr[8], fArr[9]);
            matrixMap();
            this.lastDoubleDegress = computeDegree;
            return;
        }
        Point point = new Point((int) motionEvent.getX(), (int) motionEvent.getY());
        float[] fArr2 = this.dstPs;
        float computeDegree2 = computeDegree(point, new Point((int) fArr2[8], (int) fArr2[9]));
        float[] fArr3 = this.dstPs;
        this.mMatrix.postRotate(computeDegree2 - this.lastDegree, fArr3[8], fArr3[9]);
        matrixMap();
        this.lastDegree = computeDegree2;
    }

    public float getScaleValue() {
        float[] fArr = this.srcPs;
        float f = ((fArr[8] - fArr[0]) * (fArr[8] - fArr[0])) + ((fArr[9] - fArr[1]) * (fArr[9] - fArr[1]));
        float[] fArr2 = this.dstPs;
        return (float) Math.sqrt((double) ((((fArr2[8] - fArr2[0]) * (fArr2[8] - fArr2[0])) + ((fArr2[9] - fArr2[1]) * (fArr2[9] - fArr2[1]))) / f));
    }

    private void matrixMap() {
        this.mMatrix.mapPoints(this.dstPs, this.srcPs);
        postInvalidate();
    }

    public void setUsing(boolean z) {
        this.isUsing = z;
        postInvalidate();
    }

    public boolean isUsing() {
        return this.isUsing;
    }

    private float getDistanceOfTwoPoints(float f, float f2, float f3, float f4) {
        float f5 = f - f3;
        float f6 = f2 - f4;
        return (float) Math.sqrt((double) ((f5 * f5) + (f6 * f6)));
    }

    public float computeDegree(Point point, Point point2) {
        float f = 0;
        float f2 = (float) (point.x - point2.x);
        float f3 = (float) (point.y - point2.y);
        float asin = (float) ((Math.asin(((double) f2) / Math.sqrt((double) ((f2 * f2) + (f3 * f3)))) * 180.0d) / 3.141592653589793d);
        if (!Float.isNaN(asin)) {
            int i = (f2 > 0.0f ? 1 : (f2 == 0.0f ? 0 : -1));
            if (i >= 0 && f3 <= 0.0f) {
                return asin;
            }
            int i2 = (f2 > 0.0f ? 1 : (f2 == 0.0f ? 0 : -1));
            if (i2 <= 0 && f3 <= 0.0f) {
                return asin;
            }
            if (i2 <= 0 && f3 >= 0.0f) {
                f = -180.0f;
            } else if (i >= 0 && f3 >= 0.0f) {
                f = 180.0f;
            }
            return f - asin;
        }
        return 0.0f;
    }

    private class StickerGestureListener extends GestureDetector.SimpleOnGestureListener {
        private StickerGestureListener() {
        }

        public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
            int i = clickType.ordinal();
            if (i == 1) {
                BitmapSticker.this.delete();
            } else if (i == 2) {
                BitmapSticker.this.mirror();
            }
            return true;
        }

        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            int i = clickType.ordinal();
            if (i != 3) {
                if (i == 5) {
                    if (motionEvent2.getPointerCount() == 2) {
                        if (BitmapSticker.this.downX1 + BitmapSticker.this.downY1 + BitmapSticker.this.downX2 + BitmapSticker.this.downY2 == 0.0f) {
                            BitmapSticker.this.setDoubleDownPoints(motionEvent2.getX(0), motionEvent2.getY(0), motionEvent2.getX(1), motionEvent2.getY(1));
                        }
                        BitmapSticker.this.controller(motionEvent2);
                    } else if (motionEvent2.getPointerCount() == 1) {
                        BitmapSticker.this.move(-f, -f2);
                    }
                }
            } else if (motionEvent2.getPointerCount() <= 1) {
                BitmapSticker.this.controller(motionEvent2);
            }
            return true;
        }

        public boolean onDown(MotionEvent motionEvent) {
            BitmapSticker.this.isChecked = true;
            BitmapSticker.this.calculateClickType((int) motionEvent.getX(), (int) motionEvent.getY());
            if (BitmapSticker.this.clickType == ClickType.IMAGE) {
                BitmapSticker.this.top();
            }
            return true;
        }
    }
}
